/**
 * created by 小卷毛, 2020/10/26
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.sgcc.pda.bluetooth.bt;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;

/**
 * 服务端监听和连接线程，只连接一个设备
 */
public class BtServer extends BtBase {

    private static final String TAG = BtServer.class.getSimpleName();
    private BluetoothServerSocket mSSocket;

    public BtServer(Listener listener) {
        super(listener);
        listen();
    }

    /**
     * 监听客户端发起的连接
     */
    public void listen() {
        try {
            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
            // 加密传输，Android强制执行配对，弹窗显示配对码
            // mSSocket = adapter.listenUsingRfcommWithServiceRecord(TAG, SPP_UUID);
            // 明文传输(不安全)，无需配对
            mSSocket = adapter.listenUsingInsecureRfcommWithServiceRecord(TAG, SPP_UUID);
            // 线程池开启子线程
            Util.EXECUTOR.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        // 监听连接
                        BluetoothSocket socket = mSSocket.accept();
                        // 关闭监听，只连接一个设备
                        mSSocket.close();
                        // 循环读取
                        loopRead(socket);
                    } catch (Throwable e) {
                        e.printStackTrace();
                        close();
                    }
                }
            });
        } catch (Throwable e) {
            e.printStackTrace();
            close();
        }
    }

    @Override
    public void close() {
        super.close();
        try {
            mSSocket.close();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

}
